package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.YlBedMapper;
import com.caifu.mapper.YlBuildingMapper;
import com.caifu.mapper.YlFloorMapper;
import com.caifu.mapper.YlRoomMapper;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlBuilding;
import com.caifu.pojo.YlFloor;
import com.caifu.service.IFloorService;
import com.caifu.util.Constance;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FloorServiceImpl implements IFloorService {

    @Resource
    private YlBuildingMapper buildingMapper;
    @Resource
    private YlFloorMapper floorMapper;

    @Resource
    private YlRoomMapper roomMapper;

    @Resource
    private YlBedMapper bedMapper;

    @Override
    public JSONObject queryBuildingInfo(String buildingNo) throws Exception {
        JSONObject obj = new JSONObject();
        QueryWrapper<YlBuilding> buildingQueryWrapper = new QueryWrapper<>();
        buildingQueryWrapper.eq("BUILDING_NO",buildingNo);
        YlBuilding building = buildingMapper.selectOne(buildingQueryWrapper);
        if(null!=building){
            obj.put("buildingNo",building.getBuildingNo());
            obj.put("buildingName",building.getBuildingName());
            //查询楼层数
            int floorCount = floorMapper.queryTotalFloorCounts(buildingNo);
            if(floorCount>0){
                obj.put("floorCount",floorCount);
            }else {
                obj.put("floorCount","?");
            }
            //查询总床位数
            int totalBed = bedMapper.queryBedCountByBuildingNo(buildingNo+"",null);
            if(totalBed>0){
                obj.put("totalBed",totalBed);
            }else {
                obj.put("totalBed","?");
            }
            //查询空床位数
            int emptyBed = bedMapper.queryBedCountByBuildingNo(buildingNo+"","0");
            if(emptyBed>0){
                obj.put("emptyBed",emptyBed);
            }else {
                obj.put("emptyBed","?");
            }
        }
        return obj;
    }

    @Override
    public JSONObject queryFloorDate(String buildingNo) throws Exception {
        JSONObject obj = new JSONObject();
        int floorCount = 0;
        try {
            QueryWrapper<YlFloor> floorQueryWrapper = new QueryWrapper<>();
            floorQueryWrapper.eq("BUILDING_NO",buildingNo);
            floorQueryWrapper.eq("FLOOR_STATE","1");
            floorQueryWrapper.orderByAsc("FLOOR_NAME");
            floorQueryWrapper.orderByDesc("CREATE_TIME");
            JSONArray dataArray = new JSONArray();
            List<YlFloor> floorList = floorMapper.selectList(floorQueryWrapper);
            if(null!=floorList&&floorList.size()>0){
                floorCount = floorList.size();
                for (YlFloor f: floorList
                     ) {
                    JSONObject data = new JSONObject();
                    String floorNo = Constance.Null2EmptyTrim(f.getFloorNo());
                    data.put("floorName",f.getFloorName());
                    data.put("floorNo",floorNo);
                    data.put("floorUse",f.getFloorUse());
                    //查询总房间数
                    int roomCount = roomMapper.queryTotalRoom(floorNo);
                    if(roomCount>0){
                        data.put("roomCount",roomCount);
                    }else {
                        data.put("roomCount","?");
                    }
                    //查询总房间数
                    int totalBed =bedMapper.queryBedCountByFloorNo(floorNo,null);
                    if(totalBed>0){
                        data.put("totalBed",totalBed);
                    }else {
                        data.put("totalBed","?");
                    }
                    //查询空房间数
                    int emptyBed =bedMapper.queryBedCountByFloorNo(floorNo,"0");
                    if(emptyBed>0){
                        data.put("emptyBed",emptyBed);
                    }else {
                        data.put("emptyBed","?");
                    }
                    dataArray.add(data);
                }
            }
            obj.put("code","0000");
            obj.put("codeDesc","查询成功");
            obj.put("floorCount",floorCount);
            obj.put("data",dataArray);
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("floorCount",0);
            return obj;
        }
    }

    @Override
    public JSONObject saveFloorInfo(YlFloor floor, SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        if(null==user){
            result.put("code","1001");
            result.put("codeDesc","登录超时，请重新登录");
            return result;
        }
        if(null==floor){
            result.put("code","1002");
            result.put("codeDesc","楼层信息为空");
            return result;
        }
        try {
            String floorName = Constance.Null2EmptyTrim(floor.getFloorName());
            String buildingNo =  Constance.Null2EmptyTrim(floor.getBuildingNo());
            //验证楼层名称是否存在
            YlFloor exitFloor = floorMapper
                    .queryFloorInfoByFloorName(floorName,buildingNo);
            if(null!=exitFloor){
                result.put("code","1003");
                result.put("codeDesc","该楼层名称已存在！");
                return result;
            }
            floor.setTellerNo(user.getUserNo()+"");
            floor.setFloorState("1");
            floor.setCreateTime(Constance.currentDateTime());
            floorMapper.insert(floor);
            result.put("code","0000");
            result.put("codeDesc","添加成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","添加失败！");
            return result;
        }
    }

    @Override
    public YlFloor queryFloorInfo(String floorNo) throws Exception {
        return floorMapper.selectById(floorNo);
    }

    @Override
    public JSONObject updateFloorInfo(YlFloor floor) throws Exception {
        JSONObject result = new JSONObject();
        if(null==floor){
            result.put("code","1001");
            result.put("codeDesc","楼层信息为空");
            return result;
        }
        try {
            //查询原楼层信息
            YlFloor oldFloor = queryFloorInfo(floor.getFloorNo()+"");
            if(null==oldFloor){
                result.put("code","1002");
                result.put("codeDesc","楼层信息不存在！");
                return result;
            }
            String oldFloorName = Constance.Null2EmptyTrim(oldFloor.getFloorName());
            String newFloorName = Constance.Null2EmptyTrim(floor.getFloorName());
            if(!oldFloorName.equals(newFloorName)){
                //验证楼层编号是否重复
                //验证楼层名称是否存在
                YlFloor exitFloor = floorMapper
                        .queryFloorInfoByFloorName(newFloorName
                                ,oldFloor.getBuildingNo()+"");
                if(null!=exitFloor){
                    result.put("code","1003");
                    result.put("codeDesc","该楼层名称已存在！");
                    return result;
                }
            }
            oldFloor.setFloorName(floor.getFloorName());
            oldFloor.setFloorUse(floor.getFloorUse());
            floorMapper.updateById(oldFloor);
            result.put("buildingNo",oldFloor.getBuildingNo());
            result.put("code","0000");
            result.put("codeDesc","修改成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","修改失败！");
            return result;
        }
    }

    @Override
    public JSONObject deleteFloorInfo(String floorNo) throws Exception {
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(floorNo)){
            result.put("code","1001");
            result.put("codeDesc","楼层编号为空！");
            return result;
        }
        try {
            //查询原楼层信息
            YlFloor oldFloor = queryFloorInfo(floorNo);
            if(null==oldFloor){
                result.put("code","1002");
                result.put("codeDesc","楼层信息不存在！");
                return result;
            }
            //查询楼层下的房间数
            int roomCount =roomMapper.queryTotalRoom(floorNo);
            if(roomCount>0){
                result.put("code","1003");
                result.put("codeDesc","该楼层已创建房间，无法删除！");
                return result;
            }
            oldFloor.setFloorState("9");
            floorMapper.updateById(oldFloor);
            result.put("code","0000");
            result.put("codeDesc","删除成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","删除失败");
            return result;
        }
    }
}
